﻿using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_mapping_between_nullable_types_and_normal_types
    {
        private Mapping<DomainPerson, GuiPerson> mapping;
        private DomainPerson domainPerson;
        private GuiPerson guiPerson;

        public class DomainPerson 
        {
            public int Age { get; set; }
        }

        public class GuiPerson
        {
            public int? Age { get; set; }
        }

        public When_mapping_between_nullable_types_and_normal_types()
        {            
            mapping = new Mapping<DomainPerson,GuiPerson>();
            mapping.Relate(domain=>domain.Age,gui=>gui.Age);
        }
        
        [Fact]
        public void Should_do_the_mapping_towards_nullable()
        {
            domainPerson = new DomainPerson { Age = 35 };
            guiPerson = new GuiPerson { Age = null };

            mapping.Map(domainPerson, guiPerson);
            Assert.Equal(35,guiPerson.Age);
        }

        [Fact]
        public void Should_do_the_mapping_towards_normal_type()
        {
            domainPerson = new DomainPerson { Age = 0 };
            guiPerson = new GuiPerson { Age = 35 };

            mapping.Map(guiPerson, domainPerson);
            Assert.Equal(35, domainPerson.Age);
        }

        [Fact]
        public void Should_do_the_mapping_towards_normal_type_and_default_null_values()
        {
            domainPerson = new DomainPerson { Age = 35 };
            guiPerson = new GuiPerson { Age = null };

            mapping.Map(guiPerson,domainPerson);
            Assert.Equal(0, domainPerson.Age);
        }
    }
}
